package com.audaque.springboot.foshanupload.web.webdemo.controller;

import com.audaque.springboot.foshanupload.log.anno.WebLogAnno;
import com.audaque.springboot.foshanupload.web.webdemo.model.domain.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


/**
 * @author zgb
 * @desc ...
 * @date 2022-01-25 22:13:31
 */
@Slf4j
@RestController
@RequestMapping("jacksonSerialize")
public class JacksonController {

    @WebLogAnno
    @GetMapping("/toJsonStr")
    public String toJsonStr() throws Exception {
        Student s = new Student();
        s.setName("5wimming");
        s.setSex(2);
        s.setMyObject(new Monitor());
        ObjectMapper mapper = new ObjectMapper();
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT); //compare
        return mapper.writeValueAsString(s);
    }

    @WebLogAnno
    @GetMapping("/parse")
    public Student parse() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT); //compare
        return mapper.readValue(toJsonStr(), Student.class);
    }


    @WebLogAnno
    @GetMapping("/toJsonStr1")
    public String toJsonStr1() throws Exception {
        Student1 s = new Student1();
        s.setName("5wimming");
        s.setSex(2);
        s.myObject = new Monitor();
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(s);
    }

    @WebLogAnno
    @GetMapping("/parse1")
    public Student1 parse1() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(toJsonStr1(), Student1.class);
    }


    @WebLogAnno
    @GetMapping("/toJsonStr2")
    public String toJsonStr2() throws Exception {
        Student s = new Student();
        s.setName("5wimming");
        s.setSex(2);
        s.setMyObject(new Monitor());
        ObjectMapper mapper = new ObjectMapper();
        mapper.enableDefaultTyping(); //compare
        return mapper.writeValueAsString(s);
    }

    @WebLogAnno
    @GetMapping("/parse2")
    public Student parse2() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enableDefaultTyping(); //compare
        return mapper.readValue(toJsonStr2(), Student.class);
    }




    @WebLogAnno
    @GetMapping("/toJsonStr3")
    public String toJsonStr3() throws Exception {
        Student s = new Student();
        s.setName("5wimming");
        s.setSex(2);
        s.setMyObject(new Monitor());
        List<Student> students = new ArrayList<>();
        students.add(s);
        ObjectMapper mapper = new ObjectMapper();//只能使用json
        return mapper.writeValueAsString(students);
    }




    @WebLogAnno
    @GetMapping("/parse31")
    public List<Student> parse31() throws Exception {
        ObjectMapper mapper = new ObjectMapper();//只能使用json
        JavaType javaType =mapper.getTypeFactory().constructParametricType(ArrayList.class, Student.class);
        mapper.getTypeFactory().constructCollectionType(List.class, Student.class);
        List<Student> list = mapper.readValue(toJsonStr3(), javaType);
        return list;
    }

    @WebLogAnno
    @GetMapping("/parse311")
    public List<Student> parse311() throws Exception {
        ObjectMapper mapper = new ObjectMapper();//只能使用json
        JavaType javaType =mapper.getTypeFactory().constructCollectionType(List.class, Student.class);

        Class<?> rawClass = javaType.getRawClass();
        Class<?> superclass = rawClass.getSuperclass();
        String name = rawClass.getName();
        JavaType contentType = javaType.getContentType();
        Class<?> rawClass1 = contentType.getRawClass();
        String name1 = rawClass1.getName();

        JavaType referencedType = javaType.getReferencedType();
        JavaType keyType = javaType.getKeyType();
        String typeName = javaType.getTypeName();

        List<Student> list = mapper.readValue(toJsonStr3(), javaType);
        return list;
    }



    @WebLogAnno
    @GetMapping("/parse32")
    public List<Student> parse32() throws Exception {
        TypeReference<List<Student>> typeReference = new TypeReference<List<Student>>() {};
        Type type = typeReference.getType();
        String typeName = type.getTypeName();
        ObjectMapper objectMapper = new ObjectMapper();
        List<Student> list = objectMapper.readValue(toJsonStr3(), typeReference);
        return list;
    }






}
